package com.lp.search.controller;

import com.lp.model.config.AppResult;
import com.lp.model.config.CodeMsg;
import com.lp.model.dto.search.SearchPageDto;
import com.lp.model.dto.search.SearchPrefix;
import com.lp.model.entity.article.vo.ArticleVO;
import com.lp.model.entity.article.vo.PageVO;
import com.lp.model.utils.PageUtils;
import com.lp.search.service.IArticleSearchService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article") //统一 /search 前缀(网关去掉了)
public class ArticleSearchController {

    @Autowired
    private IArticleSearchService articleSearchService;

    @PostMapping("/all")
    public AppResult search(@RequestBody @NonNull SearchPageDto pageDto) {
        if(!PageUtils.checkSearchPageDto(pageDto)) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_SEARCH_PAGE);
        }
        PageVO<ArticleVO> pageVO = articleSearchService.search(pageDto);
        return AppResult.success(pageVO);
    }

    @PostMapping("/sug")
    public AppResult suggestion(@RequestBody @NonNull SearchPrefix prefix) {
        List<String> sug = articleSearchService.suggestion(prefix);
        return AppResult.success(sug);
    }

}
